﻿using System;
using System.Collections.Generic;
using System.Text;

namespace LeetcodeTest._100._40
{
    public class Leetcode38
    {
        /*
         38. Count and Say 

        The count-and-say sequence is the sequence of integers beginning as follows:
1, 11, 21, 1211, 111221, ...

1 is read off as "one 1" or 11.
11 is read off as "two 1s" or 21.
21 is read off as "one 2, then one 1" or 1211.

Given an integer n, generate the nth sequence.

Note: The sequence of integers will be represented as a string. 
         */
        public string CountAndSay(int n)
        {
            string strRet = "1";
            for (int i = 1; i < n; i++)
            {
                strRet = Count(strRet);
            }
            return strRet;

        }

        private string Count(string n)
        {
            string strRet = "";
            int count = 1;
           
            for (int i = 0; i < n.Length - 1; i++)
            {
                if (n[i] == n[i + 1])
                {
                    count++;
                }
                else
                {
                    strRet = strRet + count + n[i];
                    count = 1;
                }
            }

            strRet = strRet + count + n[n.Length - 1];
         
            return strRet;
        } 
    }
}
